<?php
class SupplierCustomersController extends AppController {
    var $name = 'SupplierCustomers';

    var $pageTitle = 'Customer Information';

    var $viewPath = 'suppliers/admins/customers';

    var $uses = array('Customer', 'Site', 'User', 'CPS', 'UserCoH', 'CustomerSupplier');

    function beforeFilter() {
        parent::beforeFilter();
        // ใช้ใน Tab menus element ระบุ Secondary controller ที่กำลังถูกเลือก
        $this->set('parentNav', 'supplier_admins');
        $this->set('tabMenu', 'tabmenus/supplier/admins');
    }

    function display() {
        $customers = $this->CustomerSupplier->find('list', array('fields' => array('customer_id'), 'conditions' => array('CustomerSupplier.supplier_id' => $this->Auth->user('supplier_id'))));
        $this->paginate['Customer']['contain'] = array();
        $this->paginate['Customer']['conditions']['Customer.id'] = $customers;
        $this->paginate['Customer']['order'] = 'Customer.cust_no';
        parent::display('Customer');
        $this->_paging('Customer');

        // รายการ Customer ที่ User นี้รับผิดชอบ
        $customerOnHands = $this->UserCoH->listCustomerOnHand($this->Auth->user('id'));
        $this->set('customerOnHands', $customerOnHands);
    }

    function view($id) {
        $supplier_id = $this->Auth->user('supplier_id');
        $this->Customer->contain(array('CustomerSupplier' => array('conditions' => array('CustomerSupplier.supplier_id' => $supplier_id))));
        $this->data = $this->Customer->findById($id);
        $this->Customer->id = $id;
        $this->set('neighbors', $this->Customer->find('neighbors', array('fields' => array('id', 'name'),
                'conditions' => array('Customer.id' => $this->CPS->listCustomerBySupplierId($supplier_id)))));
    }

    function display_sites($customer_id) {

        $this->data = $this->Site->find('all',array(
                'conditions' => array(
                    'Site.id' => $this->CPS->listSiteBySupplierId($this->Auth->user('supplier_id'), $customer_id)
                )
        ));
        $this->set('customer_id', $customer_id);
    }

    function view_site($customer_id, $site_id) {
        $this->Site->contain();
        $this->data = $this->Site->findById($site_id);
        $this->set('customer_id', $customer_id);
    }
}
?>
